package com.github.nuggets.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.nuggets.model.entity.NuggetsDocumentEntity;
import com.github.nuggets.model.entity.State;
import com.github.nuggets.model.result.PageResult;
import com.github.nuggets.repository.DocumentRepository;
import com.github.nuggets.repository.db.NuggetsDocumentMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/22 5:19 下午
 */
@Service
public class DocumentRepositoryImpl extends ServiceImpl<NuggetsDocumentMapper, NuggetsDocumentEntity> implements DocumentRepository {


    @Override
    public Optional<NuggetsDocumentEntity> getDocumentByAccount(Long accountId, Long documentId) {

        NuggetsDocumentEntity one = this.getOne(
                new LambdaQueryWrapper<NuggetsDocumentEntity>()
                        .eq(NuggetsDocumentEntity::getAccountId, accountId)
                        .eq(NuggetsDocumentEntity::getId, documentId)
        );

        return Optional.ofNullable(one);
    }

    @Override
    public IPage<NuggetsDocumentEntity> listDocumentByAccount(Long accountId, Optional<State> state, Integer pageIndex, Integer pageSize) {

        LambdaQueryWrapper<NuggetsDocumentEntity> wrapper = new LambdaQueryWrapper<NuggetsDocumentEntity>()
                .eq(NuggetsDocumentEntity::getAccountId, accountId);

        state.ifPresent(s->wrapper.eq(NuggetsDocumentEntity::getState,state));

         return   this.page(
                new Page<>(pageIndex, pageSize),
                wrapper

        );


    }


}
